<!--
Copyright (c) 2019 The Khronos Group Inc.
Use of this source code is governed by an MIT-style license that can be
found in the LICENSE.txt file.
-->

<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>WebGL Canvas Conformance Tests</title>
<link rel="stylesheet" href="../../resources/js-test-style.css"/>
<script src="../../js/js-test-pre.js"></script>
<script src="../../js/webgl-test-utils.js"></script>
</head>
<body>
<div id="description"></div>
<div id="console"></div>
<canvas id="canvas2d_0" width="128" height="128"> </canvas>
<canvas id="canvas2d_1" width="400" height="400"> </canvas>
<canvas id="canvas2d_2" width="128" height="128"> </canvas>
<canvas id="webgl" width="400" height="400"> </canvas>
<script>
"use strict";

description("This test ensures WebGL implementations interact correctly with the canvas 2D drawImage call when drawing the same content.");

var err;
var wtu = WebGLTestUtils;

var canvas2d = [];
var ctx2d = [];
for (var i = 0; i < 3; i ++) {
  canvas2d[i] = document.getElementById("canvas2d_" + i);
  ctx2d[i] = canvas2d[i].getContext("2d");
}

var canvas = document.getElementById("webgl");
var gl = wtu.create3DContext(canvas);
if (!gl) {
  testFailed("context does not exist");
} else {
  testPassed("context exists");

  debug("Checking drawing the same WebGL content to HW accelerated canvas and SW Canvases");
  debug("");
  var color = [[0.25, 0.5, 0.75, 1], [1, 0, 0, 1], [1, 0, 1, 1]];
  var colorValue = [[64, 128, 192, 255], [255, 0, 0, 255], [255, 0, 255, 255]];
  for (var count = 0; count < 10; count ++) {
    for (var i = 0; i < 3; i++) {
      gl.clearColor(color[i][0], color[i][1], color[i][2], color[i][3]);
      gl.clear(gl.COLOR_BUFFER_BIT | gl.DEPTH_BUFFER_BIT);
      ctx2d[0].drawImage(canvas, 0, 0, canvas2d[0].width, canvas2d[0].height);
      ctx2d[1].drawImage(canvas, 0, 0, canvas2d[1].width, canvas2d[1].height);
      ctx2d[2].drawImage(canvas, 0, 0, canvas2d[2].width, canvas2d[2].height);
      wtu.checkCanvasRect(ctx2d[0], 0, 0, canvas2d[0].width, canvas2d[0].height, colorValue[i],
                          "drawImage: Should be (" + colorValue[i][0] + "," + colorValue[i][1] +
                          "," + colorValue[i][2] + "," + colorValue[i][3] + ").", 2);
      wtu.checkCanvasRect(ctx2d[1], 0, 0, canvas2d[1].width, canvas2d[1].height, colorValue[i],
                          "drawImage: Should be (" + colorValue[i][0] + "," + colorValue[i][1] +
                          "," + colorValue[i][2] + "," + colorValue[i][3] + ").", 2);
      wtu.checkCanvasRect(ctx2d[2], 0, 0, canvas2d[2].width, canvas2d[2].height, colorValue[i],
                          "drawImage: Should be (" + colorValue[i][0] + "," + colorValue[i][1] +
                          "," + colorValue[i][2] + "," + colorValue[i][3] + ").", 2);
    }
  }

  err = gl.getError();
  debug("");
}
finishTest();
</script>

</body>
</html>
